package cn.itcast.jx.service.impl;

import java.io.Serializable;
import java.util.List;
import java.util.Set;

import cn.itcast.jx.dao.BaseDao;
import cn.itcast.jx.domain.Contract;
import cn.itcast.jx.domain.ContractProduct;
import cn.itcast.jx.domain.ExtCproduct;
import cn.itcast.jx.service.ContractProductService;
import cn.itcast.jx.util.Page;
import cn.itcast.jx.util.UtilFuns;

public class ContractProductServiceImpl implements ContractProductService{

	private BaseDao baseDao;

	public void setBaseDao(BaseDao baseDao) {
		this.baseDao = baseDao;
	}

	@Override
	public Page<ContractProduct> findPage(String hql, Page<ContractProduct> page,
			Class<ContractProduct> entityClass, Object[] params) {
		return baseDao.findPage(hql, page, entityClass, params);
	}

	@Override
	public ContractProduct get(Class<ContractProduct> entityClass, Serializable id) {
		// TODO Auto-generated method stub
		return baseDao.get(entityClass, id);
	}

	@Override
	public void saveOrUpdate(ContractProduct entity) {
		double amount = 0d;
		if(UtilFuns.isEmpty(entity.getId())) {
			//新增
			
			if(UtilFuns.isNotEmpty(entity.getPrice()) && UtilFuns.isNotEmpty(entity.getCnumber())) {
				amount = entity.getPrice() * entity.getCnumber();
				entity.setAmount(amount);
			}
			Contract contract = baseDao.get(Contract.class, entity.getContract().getId());
			contract.setTotalAmount(contract.getTotalAmount() + amount);
			//修改总金额
			baseDao.saveOrUpdate(entity);
			baseDao.saveOrUpdate(contract);
		}else {
			//修改
			//更新
			Double newAmount = 0d;
			Contract contract = baseDao.get(Contract.class, entity.getContract().getId());
			ContractProduct contractProduct = baseDao.get(ContractProduct.class, entity.getId());
			
			if(UtilFuns.isNotEmpty(entity.getPrice()) && UtilFuns.isNotEmpty(entity.getCnumber())) {
				 newAmount = entity.getPrice() * entity.getCnumber();
				
			}
			contract.setTotalAmount(contract.getTotalAmount() - contractProduct.getAmount() + newAmount);
			contractProduct.setAmount(newAmount);
			baseDao.saveOrUpdate(contractProduct);
			baseDao.saveOrUpdate(contract);
		}
	}

	@Override
	public void delete(Class<ContractProduct> entityClass, String id) {
		ContractProduct contractProduct = baseDao.get(ContractProduct.class, id);
		if(contractProduct.getExtCproducts() == null) {
			Contract contract = contractProduct.getContract();
			contract.setTotalAmount(contract.getTotalAmount() - contractProduct.getAmount());
			baseDao.saveOrUpdate(contract);
			baseDao.delete(ContractProduct.class, new String[]{id});
		}else {
			Set<ExtCproduct> extCproducts = contractProduct.getExtCproducts();
			Double extAmount = 0d;
			for (ExtCproduct extCproduct : extCproducts) {
				extAmount += extCproduct.getAmount();
			}
			Contract contract = contractProduct.getContract();
			contract.setTotalAmount(contract.getTotalAmount() - contractProduct.getAmount() - extAmount);
			baseDao.saveOrUpdate(contract);
			baseDao.delete(ContractProduct.class, new String[]{id});
			
		}
	}

	@Override
	public List<ContractProduct> find(String hql, Class<ContractProduct> entityClass,
			Object[] params) {
		// TODO Auto-generated method stub
		return baseDao.find(hql, entityClass, params);
	}
	
}
